//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "Platforms/Base/IApplication.h"

#if defined( PLATFORM_WINDOWS ) and defined( ANDROID_EMULATOR )

namespace Engine
{
namespace Platforms
{


	//
	// Context EGL
	//

	struct ContextEGL
	{
	// types
	public:
		typedef bool (*InitWindowProc_t) (void *window, int format, void *parameter);
		typedef bool (*ReleaseWindowProc_t) (void *window, void *parameter);
		typedef void (*OnContextLostProc_t) (ContextEGL *context, void *parameter);

		struct Descriptor
		{
			enum EColorFormat
			{
				R8_G8_B8_A8		= 0x08080808,
				R4_G4_B4_A4		= 0x04040404,
				R5_G6_B5		= 0x05060500,

				RGBA8			= R8_G8_B8_A8,
				RGBA4			= R4_G4_B4_A4,
				RGB565			= R5_G6_B5,
			};

			enum EDepthFromat
			{
				NO_DEPTH		= 0,
				DEPTH_16		= 16,
				DEPTH_24		= 24,
				DEPTH_32		= 32,
			};

			enum EStencilFormat
			{
				NO_STENCIL		= 0,
				STENCIL_8		= 8,
			};

			enum EVersion
			{
				OPENGLES_1_0	= 0x10,
				OPENGLES_1_1	= 0x11,
				OPENGLES_2_0	= 0x20,
				OPENGLES_3_0	= 0x30,
			};

			EColorFormat	colorFormat;	// R, G, B, A size in bits
			uint8			depthBits;
			uint8			stencilBits;
			uint8			multiSamples;
			uint8			version;
			///
			Descriptor (EColorFormat colorFmt = RGBA8,
						uint8		 depth    = DEPTH_16,
						uint8		 stencil  = NO_STENCIL,
						uint8		 samples  = 0,
						uint8		 ver	  = OPENGLES_2_0 ) :
				colorFormat(colorFmt), depthBits(depth),
				stencilBits(stencil), multiSamples(samples),
				version(ver) {}
			
			void SetFormat (int r, int g, int b, int a, int d, int s, int ms)
			{
				depthBits		= d;
				stencilBits		= s;
				colorFormat		= EColorFormat( (a & 0xFF) | ((b & 0xFF) << 8) | ((g & 0xFF) << 16) | ((r & 0xFF) << 24) );
				multiSamples	= ms;
			}

			int RedBits ()		const	{ return (colorFormat >> 24) & 0xFF; }
			int GreenBits ()	const	{ return (colorFormat >> 16) & 0xFF; }
			int BlueBits ()		const	{ return (colorFormat >>  8) & 0xFF; }
			int AlphaBits ()	const	{ return (colorFormat & 0xFF); }

			uint MajorVersion()	const	{ return version >> 4; }
			uint MinorVersion()	const	{ return version & 0xF; }
		};


	// variables
	private:


	// methods
	public:
		ContextEGL () {}
		~ContextEGL () {}

		bool Initialize (Descriptor &desc);
		void Destroy ();
		
		void SetCallbacks (	InitWindowProc_t init,
							ReleaseWindowProc_t release,
							OnContextLostProc_t lostContext,
							void * parameter)
		{}

		bool CreateSurface (void *window);
		void DestroySurface ();
		bool IsSurfaceCreated () const;

		bool SwapBuffers ();
		bool SwapInterval (int interval = 1);

		bool MakeCurrent ();
		bool ResetContext ();
		bool IsContextCurrent () const;
		bool IsContextInitialized () const;

		//uvec2 SurfaceSize () const;
		//vec2  SurfaceResolution () const;

		//int  Version () const;
	};


}	// Platforms
}	// Engine

#endif	// PLATFORM_WINDOWS and ANDROID_EMULATOR